package com.sinodata.bsm.cicp.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 
 * <p>
 * Description: 存储指标值的缓存
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-5 13:34:59       liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ValueBuffer {

    private static ValueBuffer instance = new ValueBuffer();

    private final Map<String, BufferItem> _values = new HashMap<String, BufferItem>();

    private static final int TIMEOUT = 1000 * 60 * 60;

    private ValueBuffer() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    clearValueBuffer();
                    try {
                        Thread.sleep(1000 * 60 * 10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    public static ValueBuffer getInstance() {
        return instance;
    }

    /**
     * 向缓存中存储一个数据
     * 
     * @param key
     * @param value
     */
    public synchronized void put(String key, Object value) {
        if (value != null) {
            _values.put(key, new BufferItem(value));
        }
    }

    /**
     * 删除_values中过期的缓存数据
     */
    public synchronized void clearValueBuffer() {
        Iterator<Map.Entry<String, BufferItem>> it = _values.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, BufferItem> entry = it.next();
            if (entry.getValue().isStale(TIMEOUT)) {
                it.remove();
            }
        }
    }

    /**
     * 向缓存中放入永不过期的值
     * 
     * @param key
     * @param value
     */
    public synchronized void putPermanent(String key, String[] value) {
        if (value != null) {
            _values.put(key, new BufferItem(value, true));
        }
    }

    /**
     * 从缓存中获取一个数据
     * @param key
     * @return
     */
    public Object get(Object key, int timeout) {
        BufferItem item = _values.get(key);
        if (item == null || item.isStale(timeout)) {
            return null;
        } else {
            return item._value;
        }
    }

    public void clear() {
        _values.clear();
    }

    /*
     * 缓存中的一个缓存数据
     */
    private static class BufferItem {

        private final Object _value;
        private final long _create_time;
        private boolean _permanent;

        BufferItem(Object value) {
            _value = value;
            _create_time = System.currentTimeMillis();
        }

        BufferItem(Object value, boolean isPermanent) {
            _value = value;
            _create_time = System.currentTimeMillis();
            _permanent = isPermanent;
        }

        /**
         * 判断一个缓存数据是否已经过期不能再用
         * 
         * @param period
         * @return
         */
        final boolean isStale(int timeout) {
            if (_permanent) {
                return false;
            }
            if (System.currentTimeMillis() - _create_time > timeout) {
                return true;
            } else {
                return false;
            }
        }
    }

}
